<html>
<head>
	<meta charset='utf-8' />
	<title>后台管理</title>
	<meta http-equiv="keywords" content="求职,招聘,房屋,出租,出售,求租,求购,宠物,二手,二手车,二手物品,旧货,寻物,寻人,出兑,外兑"> 
	<meta http-equiv="description" content="便民信息网,房屋租售,宠物交易,二手物品,二手车辆,寻物寻人,出兑信息">  
	<meta name="description" content="便民信息网,房屋租售,宠物交易,二手物品,二手车辆,寻物寻人,出兑信息"/>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#nav_bottom{width:98%;margin:0 auto}
			#content{width:98%;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#nav_bottom{width:450px;margin:0 auto}
			#content{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		.info_item{color:#444;margin:15px 0;display:flex}
		.info_item > div:last-child{flex:1}
	</style>
</head>
<body style='padding:0;margin:0;background:#eee'>
<!-- 1 Content容器 -->
<div id='Content'>
	<!-- 3 内容区-->
	<div style='margin:10px 0;word-break:break-all;padding:10px;'>
		<div style='float:right;text-align:right' onclick='After_Admin_Exit()'>退出</div>

		<h2 style='text-align:center;'>后台管理-权限功能管理</h2>
		
	

		<div style='margin:10px auto'>
			<span id='after_fun_select'></span>	<!--功能页面选择-->

			<span style='float:right'>
				<button id='admin_user_add_btn'>新增功能或页面</button>
			</span>
		</div>
		<div id='admin_fun_add_area' style='display:none'>
			新增：
			<select id='admin_fun_add_area_type_select'>
				<option value="page">页面</option>
				<option value="fun">功能</option>
			</select>
			<select style='display:none' id="admin_fun_add_area_page_select">
				<option value="">页面选择</option>
				<option value="">页面1</option>
				<option value="">页面1</option>
			</select>
			<div>
				权限标识：<input type="text" id='admin_fun_add_inp_name' />(唯一,前缀none为不验证)
			</div>
			<div>
				权限说明：<input type="text" id='admin_fun_add_inp_explain' />
			</div>
			<button id='admin_fun_add_sub'>提交</button>
			<button id='admin_user_add_back'>返回</button>
		</div>

		<hr>

		<!--列表区-->
		<div id='fun_list' style='margin-bottom:100px;font-size:12px'></div>




		<!--修改密码浮动区-->
		<div id='user_pwd_change_area' style='position:fixed;top:200px;border:1px solid #AAA;background:#fff;padding:50px;display:none'>
			设置密码：
			<input type="password" id='user_pwd_change_inp' />
			<button id='user_pwd_change_sub'>提交</button>
			<button id='user_pwd_change_back'>返回</button>
		</div>

				
		<div id='user_grant_change_area' style='position:fixed;top:200px;border:1px solid #AAA;background:#fff;padding:50px;display:none'>
			<select id='user_grant_change_sel'>
				<option value="1">组1</option>
				<option value="2">组2</option>
			</select>
			<button id='user_grant_change_sub'>提交</button>
			<button id='user_grant_change_back'>返回</button>
		</div>

	</div>	<!-- 3 内容区结束-->	

</div><!-- 1 Content容器结束 -->







<!--底部导航容器-->
<div id='Nav_Bottom' style='position:fixed;bottom:0;width:100%;height:45px'>



</body>
</html>


<script src='./Public/jquery.js'></script>
<script src='./Public/fun.js'></script>
<script>
	Check_After_User_Cookie();	//检测登陆状态
	
	var priv_name = 'after_set_fun';
	Priv_Check(priv_name,function(res){
		if(res != 'ok'){
			alert('没有权限!');
			//location.href = document.referrer;
			exit;
		}
	});
	After_Fun_Select();	//加载后台设置页中的功能选择





	var uid;


	//加载底部导航栏
	Load_NavBottom('after');

	$(function(){



		//### 新增功能页面相关
		$('#admin_user_add_btn').click(function(){
			var priv_name = 'AfterSetFun_Add';
			Priv_Check(priv_name,function(res){
				if(res != 'ok'){
					alert('没有权限!');
					//location.href = document.referrer;
					exit;
				}
			});
			$(this).hide();
			$('#admin_fun_add_area').show();
		});

		//判断新增的是页面还是功能,是功能的话加载页面数据
		$('#admin_fun_add_area_type_select').change(function(){
			var val = $(this).find('option:selected').val();
			if(val == 'fun'){
				$('#admin_fun_add_area_page_select').show();
				var type = 'After_Fun_Get_Page_Data';
				Token(type,function(t){
					$.ajax({
						url:url,
						type:'post',
						data:{
							type:type,
							token:t
						},
						success(res){
							var page_list = JSON.parse(res);
							var page_list_str = '';
							page_list_str += "<option value='none'>选择页面</option>";
							for(var i=0;i<page_list.length;i++){
								page_list_str += "<option value='"+page_list[i][0]+"'>"+page_list[i][2]+"</option>";
							}
							$("#admin_fun_add_area_page_select").html(page_list_str);
						}
					});
				});
			}else{
				$('#admin_fun_add_area_page_select').hide();
			}
		});


		//新增功能的提交
		$('#admin_fun_add_sub').click(function(){
			var name = $('#admin_fun_add_inp_name').val().trim();
			var explain = $('#admin_fun_add_inp_explain').val().trim();
			if(!name || !explain){
				alert('不得为空！');return;
			}
			if($('#admin_fun_add_area_type_select').find('option:selected').val() == 'page'){
				var parent = 0;
			}else{
				var parent = $('#admin_fun_add_area_page_select').find('option:selected').val();
				if(parent == 'none'){
					alert('请选择页面！');return;
				}
			}
			name = ChangeChar(name); 	//检测并转换特殊字符
			explain = ChangeChar(explain);
			var type = 'Admin_fun_Add';
			Token(type,function(t){
				$.ajax({
					url:url,
					type:'post',
					data:{
						token:t,
						type:type,
						name:name,
						explain:explain,
						parent:parent
					},
					success(res){
						if(res == 'ok'){
							$('#admin_fun_add_area_type_select').val('page');
							$('#admin_fun_add_area_page_select').hide();
							$('#admin_fun_add_inp_name').val('');
							$('#admin_fun_add_inp_explain').val('');
							alert('操作成功！');
							$('#admin_fun_add_area').hide();
							$('#admin_user_add_btn').show();
							After_Get_Fun_List();	//获取功能列表 
						}else{
							alert('操作失败');
						}
					}
				});
			});
		});

		//返回
		$('#admin_user_add_back').click(function(){
			$('#admin_fun_add_area').hide();
			$('#admin_user_add_btn').show();
		});


		After_Get_Fun_List();	//获取功能列表 

	});









	//获取功能列表 
	function After_Get_Fun_List(){
		var type = 'After_Get_Fun_List';
		Token(type,function(t){
			$.ajax({
				url:url,
				type:'post',
				data:{
					token:t,
					type:type
				},
				success(res){
					$('#fun_list').html('');
					//alert(res);
					if(res == 'err'){
						return;
					}
					var data = JSON.parse(res);
					//alert(data.privilege[0]);
					var list = [];
					for(var i=0;i<data.length;i++){
						//alert(data[i]);return;
						var obj = new Object();
						if(data[i][3] == 0){	//如果是页面
							obj.page = data[i];
							list.push(obj);
						}
					}
					//alert(list[0].page);return;
					for(var j=0;j<list.length;j++){
						//alert(list[j].page[0]);return;
						var tmp_arr = [];
						for(var i=0;i<data.length;i++){
						//alert(data.funs[i][3]);
							if(list[j].page[0] == data[i][3]){
								tmp_arr.push(data[i]);
							}
						}
						list[j].fun = tmp_arr;
					}
					//alert(list[0].fun[0][2]);return;
					var list_str = '';
					for(var i=0;i<list.length;i++){
						list_str += "<div style='margin:20px 0'>";
						list_str += "页面："+list[i].page[2]+"-"+list[i].page[1]+"　<button class='Del_Fun_Btn' id="+list[i].page[0]+" type='page'>删除</button>";
						for(var j=0;j<list[i].fun.length;j++){
							list_str += "<div style='margin:5px;text-indent:10px'>";
							list_str += "功能："+list[i].fun[j][2]+"-"+list[i].fun[j][1]+"　<button class='Del_Fun_Btn' id="+list[i].fun[j][0]+" type='fun'>删除</button>";
							list_str += "</div>";
						}
						list_str += "</div>";

					}
		
					//$('#funs_list').html(list_str);
					$('#fun_list').html(list_str);


					//删除功能或页面
					$('.Del_Fun_Btn').click(function(){
						var priv_name = 'AfterSetFun_Del';
						Priv_Check(priv_name,function(res){
							if(res != 'ok'){
								alert('没有权限!');
								//location.href = document.referrer;
								exit;
							}
						});
						if(!confirm('确认删除操作?')){
							return;
						}
						var fid = $(this).attr('id');
						var page_stat = $(this).attr('type');
						var pwd = prompt('输入口令!');
						var type = 'After_Del_Con_Pwd_Diff';
						Token(type,function(t){
							$.ajax({
								url:url,
								type:'post',
								data:{
									token:t,
									type:type,
									pwd:pwd
								},
								success(res){
									if(res == 'ok'){
										Fun_Del(fid,page_stat);
									}else{
										alert('口令不正确!');
									}
								}
							});
						});

					});

				}
			});
		});
	}


	function Fun_Del(fid,page_stat){
		var type = 'After_Fun_Del';
		Token(type,function(t){
			$.ajax({
				url:url,
				type:'post',
				data:{
					token:t,
					type:type,
					fid:fid,
					page_stat:page_stat
				},
				success(res){
					if(res == 'ok'){
						alert('操作成功!');
						After_Get_Fun_List();
					}
				}
			});
		});
	}

</script>

